package lab_08.com.acme.task1;

public class Calculator {
	public static void main(String[] args) {
		double operand1,operand2 = 0;
				
		// 3 arguments requirement
		if (args.length != 3) {
			System.out.format("The number of arguments should be 3. You gave %1$s.", args.length);
			return;
		}
		
		
		// Parsing operators
		try {
			operand1 = Double.parseDouble(args[0]);
		} catch (Exception e) {
			System.out.format("First operand '%1$s' is not a number.", args[0]);
			return;
		}
		
		try {
			operand2 = Double.parseDouble(args[2]);
		} catch (Exception e) {
			System.out.format("Second operand '%1$s' is not a number.", args[2]);
			return;
		}
		
		// Analyzing operator and making calculations
		if (args[1].length() != 1) {
			System.out.format("Operator '%1$s' is not one of +, - , *, /", args[1]);
			return;
		}
		
		double result = 0;
		
		switch (args[1].charAt(0)) {
		case '+':
			result = operand1 + operand2;
			break;
		case '-':
			result = operand1 - operand2;
			break;
		case '*': //For some reason args have 9 arguments when * is provided.
			result = operand1 * operand2;
			break;
		case '/':
            if (operand2==0) {
            	System.out.format("You are dividing by zero.");
            	return;
            }
			result = operand1 / operand2;
			break;
		default:
			System.out.format("Operator '%1$s' is not one of +, - , *, /", args[1]);
			return;
		}
		

		
		System.out.println(result);
		

	}
}
